package com.monitorRoutine.monitorRoutineServer.task;

import com.monitorRoutine.monitorRoutineServer.service.MonitorSqlService;
import com.monitorRoutine.monitorRoutineServer.util.TimeUtil;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Component
@EnableScheduling
@Slf4j
@Log
public class MonitorSqlServiceTask {
    @Autowired
    MonitorSqlService monitorSqlService;
    /**
     * 扫描是否存在命令
     */
    @Scheduled(cron = "0 */1 * * * *")
    void scanSqlRecordDatabase(){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
        Instant startTime = Instant.now();
        log.info("sql命令时间扫描，时间："+TimeUtil.formatInstant(startTime, formatter));
                monitorSqlService.scanSqlRecordDatabase();
        Instant endTime = Instant.now();
        long executionTime = Duration.between(startTime, endTime).toMillis();
        log.info("sql命令时间扫描 完成，时间："+TimeUtil.formatInstant(endTime, formatter)+"用时:"+executionTime);
    }

    @Scheduled(cron = "0 */1 * * * *")
    void executeSql(){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
        Instant startTime = Instant.now();
        log.info("sql命令执行，时间："+TimeUtil.formatInstant(startTime, formatter));
        monitorSqlService.executeSqlTask();
        Instant endTime = Instant.now();
        long executionTime = Duration.between(startTime, endTime).toMillis();
        log.info("sql命令执行 完成，时间："+TimeUtil.formatInstant(endTime, formatter)+"用时:"+executionTime);
    }
}
